package curator;


import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CuratorDelete {
    private String IP="192.168.0.201:2181,192.168.0.202:2181,192.168.0.203:2181";
    CuratorFramework client;

    @Before
    public void createClient(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString(IP)
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .namespace("delete")
                .build();
        client.start();
    }

    @Test
    public void test01() throws Exception {
        client.delete().forPath("/node1/node11");
        System.out.println("结束");
    }

    @Test
    public void test02() throws Exception {
        //版本号
        client.delete().withVersion(0).forPath("/node1/node11");
        System.out.println("结束");
    }


    @Test
    public void test03() throws Exception {
        //删除包含子节点的节点
        client.delete().deletingChildrenIfNeeded().forPath("/node1");
        System.out.println("结束");
    }

    @Test
    public void test04() throws Exception {
        //异步删除节点
        client.delete().inBackground(new BackgroundCallback() {
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                System.out.println(event.getPath());
                System.out.println(event.getType());
            }
        }).forPath("/node1");
        System.out.println("结束");
        Thread.sleep(5000);
    }

    @After
    public void closeClient(){
       client.close();
    }
}
